package com.fili.core;

import java.util.ArrayList;
import java.util.Date;
import java.io.*;

/**
 * Created by IntelliJ IDEA.
 * User: fili
 * Date: Sep 25, 2006
 * Time: 9:12:50 PM
 * To change this template use File | Settings | File Templates.
 */
public class UscatorData
{
    private int ID;
    private ArrayList<UscDataRec> data;
    private ObjectOutputStream oos;
    private UscatorView view;

    public int getID()
    {
        return ID;
    }

    public ArrayList<UscDataRec> getData()
    {
        return data;
    }

    public ArrayList<UscDataRec> getData(Date d1, Date d2)
    {
        ArrayList<UscDataRec> d = new ArrayList<UscDataRec>(10);
        for (UscDataRec udr : data)
            if (udr.data.after(d1) && udr.data.before(d2))
                d.add(udr);

        return d;
    }

    public void addData(UscDataRec rec)
    {
        data.add(rec);
        try
        {
            oos.writeObject(rec);
            oos.flush();
        }
        catch (IOException e)
        {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    public void attachView(UscatorView view)
    {
        this.view = view;
    }

    public UscatorData(int ID, String fileName)
    {
        this.ID = ID;

        try
        {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName));
            UscDataRec udr;
            do
            {
                udr = (UscDataRec) ois.readObject();
                if (udr != null)
                    data.add(udr);
            }
            while (udr != null);

            oos = new ObjectOutputStream(new FileOutputStream(fileName,true));
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
    }
}
